package com.example.springboot_shiro.service.impl;

import com.example.springboot_shiro.dao.UserDao;
import com.example.springboot_shiro.entity.Pers;
import com.example.springboot_shiro.entity.Role;
import com.example.springboot_shiro.entity.User;
import com.example.springboot_shiro.service.UserService;
import com.example.springboot_shiro.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public void register(User user) {
        //明文密码进行md5+salt+hash散列
        //1.生成随机盐
        String salt = SaltUtils.getSalt(8);
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        userDao.save(user);
    }

    @Override
    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }


  @Override
  public User findRolesByUserName(String username) {
        return userDao.findRolesByUserName(username);
    }

    @Override
    public List<Pers> findPersByRoleId(String id) {
        return userDao.findPersByRoleId(id);
    }
}
